<template>
  <div>
    <el-select
      v-model="id"
      :multiple="false"
      collapse-tags
      style="width: 100%"
      filterable
      clearable
      remote
      :size="size"
      :remote-method="handleSearch"
      @change="handleChange"
      :class="optionClass"
      @visible-change="handleVisible"
      :placeholder="$t('labels.pleaseSelect')"
    >
      <el-option
        v-for="item in datas"
        :key="item.id"
        :label="item.name"
        :value="item.id"
        :class="optionClass"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
import { getTagItems } from "@/api/tag";
export default {
  data() {
    return {
      datas: [],
    };
  },
  model: {
    prop: "id",
    event: "innerChange",
  },
  methods: {
    getData(keyword) {
      if (!this.tagId) return;
      getTagItems(this.tagId, {
        pageIndex: 1,
        pageSize: 50,
        keyword: keyword,
      }).then((res) => {
        this.datas = res ? res : [];
      });
    },
    handleChange() {
      this.$emit("innerChange", this.id);
      let selected = this.datas.find((x) => {
        return x.id == this.id;
      });
      this.$emit("change", selected);
    },
    handleSearch(keyword) {
      setTimeout(() => {
        this.getData(keyword);
      }, 200);
    },
    handleVisible(visible) {
      this.$emit("visible-change", visible);
    },
  },
  watch: {},
  mounted() {
    this.getData();
  },
  props: {
    id: {
      type: String,
    },
    tagId: {
      type: String,
    },
    size: {
      type: String,
      default: "small",
    },
    optionClass: {
      type: String,
      default: "",
    },
  },
};
</script>
